<?php
/*
 * Creative Bulletin Board Generation 3
 * Version 1.0 Beta 1
 +------------------------------------
 * User Application, Login Module (Frontend)
 * Started: 30 August, 2009 16:56 PM
 * Script version: 1.0
 +------------------------------------
 */

class module implements module_interface{
	public function returninitvars(){
		global $lang;
		$this->info["page_title"] = $lang->getByKey("user.login");
		$this->info["page_description"] = "";
		$this->info["module_version"] = "1.0";
		return $this->info;
	}

	public function construct(){ # call our initialize functions. Rule: Never, Ever output in this function.
		switch((isset($_GET["mode"]) ? $_GET["mode"] : "form")){
			case 'inline':
				$this->callInline = true;
			break;
			
			case 'form':
				$this->form = true;
			break;
		}
	}
	
	public function run(){
		global $view, $config, $db, $main, $lang;
		if(isset($this->callInline)){
			if($db->conf_value("use_recaptcha_login") == "true"){
				require_once(LIBRARY_PATH."recaptcha.php");
				$resp = recaptcha_check_answer(null,
				$_SERVER["REMOTE_ADDR"],
				$_POST["recaptcha_challenge_field"],
				$_POST["recaptcha_response_field"]);
				if(!$resp->is_valid){
					$view->writeIn($view->inlineError($lang->getByKey("core.captcha.fail")));
				}
				else{
					if(isset($_POST["username"], $_POST["password"])){
						if(($user = new user($_POST['username'], "username")) !== false){
							if($user->getStatus() == true){
								if(strcmp($main->makePassword(stripslashes($_POST['password'])), $user->getPassHash()) == 0){
									$_SESSION["cf_username"] = $_POST['username'];
									// Write UserController...
									$userController = new user($_SESSION["cf_username"], "username");
									if($userController->status === false) unset($_SESSION["cf_username"]);
									$_SESSION["cf_controller_update_timestamp"] = time();
									$_SESSION["cf_controller"] = serialize($userController);
									$view->writeIn($view->inlineSuccess($lang->getByKey("user.login.success"), "index.php"));
								}
								else{
									$view->writeIn($view->inlineError($lang->getByKey("user.login.error.password")));
								}
							}
							else{
								$view->writeIn($view->inlineError($lang->getByKey("user.login.error.username")));
							}
						}
						else{
							$view->writeIn($view->inlineError($lang->getByKey("core.error.internal")));
						}
					}
					else{
						$view->writeIn($view->inlineError($lang->getByKey("user.login.error.form")));
					}
				}
			}
			else{
				if(isset($_POST["username"], $_POST["password"])){
					if(($user = new user($_POST['username'], "username")) !== false){
						if($user->getStatus() == true){
							if(strcmp($main->makePassword(stripslashes($_POST['password'])), $user->getPassHash()) == 0){
								$_SESSION["cf_username"] = $_POST['username'];
								$view->writeIn($view->inlineSuccess($lang->getByKey("user.login.success"), "index.php"));
							}
							else{
								$view->writeIn($view->inlineError($lang->getByKey("user.login.error.password")));
							}
						}
						else{
							$view->writeIn($view->inlineError($lang->getByKey("user.login.error.username")));
						}
					}
					else{
						$view->writeIn($view->inlineError($lang->getByKey("core.error.internal")));
					}
				}
				else{
					$view->writeIn($view->inlineError($lang->getByKey("user.login.error.form")));
				}
			}
		}
		if(!isset($_SESSION["cf_username"])){
			if($db->conf_value("use_recaptcha_login") == "true"){
				require_once(LIBRARY_PATH."recaptcha.php");
				$data['captcha'] = "<br />".$lang->getByKey("core.captcha.verification").":<br />";
				$data['captcha'] .= recaptcha_get_html();
			}
			else{
				$data['captcha'] = "";
			}

			$view->writeIn(
				$view->tablePadded($lang->getByKey("user.login"), $view->showTemplate("loginform", $data), "application_form")
			);
		}
	}

	public function destruct(){}
}
